package com.dormitory_15.dormitory.Service.Impl.Student;

import com.dormitory_15.dormitory.Dao.Student.StudentDao;
import com.dormitory_15.dormitory.Entity.Student;
import com.dormitory_15.dormitory.Service.Student.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Override
    public Student login(String username, String password) {
        if(username == null || password == null){
            return null;
        }
        Student userInfo = studentDao.login(username, password);
        if(userInfo == null){
            return null;
        }
        return userInfo;

    }

    @Override
    public Integer register(Student student) {
        if(student.getName() == null && student.getIdNumber() ==null &&
                student.getCollege() == null && student.getMajor() == null){
            return null;
        }
        Student byUsername = findByUsername(student.getUsername());
        if(byUsername != null){
            return null;
        }
        Integer register = studentDao.register(student);
        if(register == null){
            return null;
        }
        return register;
    }
    /*
    * 根据账号查找是否存在用户
    * */
    @Override
    public Student findByUsername(String username) {
        if(username == null){
            return null;
        }
        return studentDao.findByUsername(username);
    }

    @Override
    public List<Student> findByName(String name, String origin) {
        if(name == null && origin == null){
            return null;
        }
        List<Student> info = studentDao.findByName(name,origin);
        if(info == null){
            return null;
        }
        return info;
    }

    @Override
    public PageInfo<Student> getStudentsByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = studentDao.selectAllStudents();
        return new PageInfo<>(students);
    }
}
